{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "905c14fa",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 本学习记录人：戴祥哲\n",
    "* 学习时间：2021-11-11\n",
    "* week06\n",
    "* [参考文档](https://python.org/doc)\n",
    "* [徐老师博客](https://cnblogs.com/xuzhichao)\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a12ef0fe",
   "metadata": {},
   "source": [
    "# 回顾上周内容：\n",
    "\n",
    "## 1. 认识列表：[value1，value2，value3]\n",
    "\n",
    "## 2. 列表的查询：\n",
    "\n",
    "> 1. 索引值进行查询(单个值)：index\n",
    "> 2. 切片进行查询（多值）：slice\n",
    "\n",
    "## 3. 列表的一般方法\n",
    "\n",
    "> 1. 增加元素\n",
    ">> 1. append() 增加元素到列表到末尾：L.append(object)\n",
    ">> 2. insert() 指定位置增加元素：L.insert(index)\n",
    "> 2. 删除元素\n",
    ">> 1. remove() 按照 值 进行删除：L.remove(value)\n",
    ">> 2. pop() 按照 索引值 进行删除：L.pop(index) 注意：不写index 默认删除列表的最后一个元素\n",
    "> 3. 修改内容\n",
    "> 4. 查询内容（参考上述2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6898eb3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f713b51a",
   "metadata": {},
   "source": [
    "# 列表的方法\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67fab86e",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6872398",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ac43533",
   "metadata": {},
   "source": [
    "> 1. clear()\n",
    ">> 1. 使用方法：L.clear()\n",
    ">> 2. 结果：Remove all items from list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "653cbab8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e590a78",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "22e10424",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19e9f407",
   "metadata": {},
   "source": [
    "> 2. count()\n",
    ">> 1. 使用方法：L.count(value)\n",
    ">> 2. 结果：查询某个value出现的次数(共现的次数)，通常用在计算词频、统计操作当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ecf521ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list=[88,89,68,78,88,90,93,88]\n",
    "分数_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7a1e319a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function count:\n",
      "\n",
      "count(value, /) method of builtins.list instance\n",
      "    Return number of occurrences of value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a519ad32",
   "metadata": {},
   "source": [
    "> 3. extend() 拓展\n",
    ">> 1. 使用方法：L.extend(iterable)\n",
    ">> 2. 结果：iterable 将拼接在 L 的后面(末尾)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b5afc20b",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班=[88,89,68,78,88,90,93,88]\n",
    "分数_list_数媒技术2班=[88,78,76,77,88,89,77,66]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5a5f5c8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "90c8ec55",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.extend(分数_list_数媒技术2班)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "59dd5cb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "25f5a288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等价于\n",
    "[88, 89, 68, 78, 88, 90, 93, 88]+[88, 78, 76, 77, 88, 89, 77, 66]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "320c8049",
   "metadata": {},
   "source": [
    "> 4. <font style=\"color:red\">**copy()**</font>\n",
    ">> 1. 在列表中，\"赋值\"不等价于\"复制\"\n",
    ">> 2. 真正的复制列表的两种常见方法\n",
    ">>> 1. copy()\n",
    ">>> 2. 切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "49ba7ee9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack Alex\n",
      "1406573691632 1406573303792\n"
     ]
    }
   ],
   "source": [
    "name1=\"Alex\"\n",
    "\n",
    "name2=name1\n",
    "\n",
    "name1=\"Jack\"\n",
    "\n",
    "print(name1,name2)\n",
    "print(id(name1),id(name2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dfb8566",
   "metadata": {},
   "source": [
    "* 结论是：赋值有两种情况：\n",
    "> 1. 赋值的数据类型是 不可变的数据(字符串、数值、元组)，分配不同的数据地址\n",
    "> 2. 赋值的数据类型是 可变的数据(列表)分配在同一个地址，指向同一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "68d872ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 此案例 来源：课本p72：看起来像复制，但其实不是\n",
    "first=[1,2,3,4,5,]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bed5b5d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8fc06213",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1406572264576\n"
     ]
    }
   ],
   "source": [
    "print(id(first)) # first 的 数据地址：1406572264576"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d8dbcda5",
   "metadata": {},
   "outputs": [],
   "source": [
    "second=first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6f0bef0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9085fd08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1406572264576\n"
     ]
    }
   ],
   "source": [
    "print(id(second)) # second 的 数据地址：1406572264576"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "774e2a66",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d40ea7c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd1e04f8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1471baf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "1406582556544 1406582556224\n"
     ]
    }
   ],
   "source": [
    "first=[1,2,3,4,5,]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "30b98c4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c8a411d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "125ce9e5",
   "metadata": {},
   "source": [
    "### 列表的复制(方法二)\n",
    "* 切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f1205b29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "1406574057408 1406574525568\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5,]\n",
    "second = first[:]\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c612875d",
   "metadata": {},
   "source": [
    "### 思考copy() 只复制 第一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ebde63ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]]\n",
      "1406573310080 1406573168768\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,[4,5,6]]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6e955ad4",
   "metadata": {},
   "outputs": [],
   "source": [
    "first[3].append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "867ef628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8c98b68d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e586b70",
   "metadata": {},
   "source": [
    "> 6. sort()\n",
    ">> 1. reverse=false(默认从小到大)\n",
    ">> 2. 可修改 revers=True(从小到大)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "986904dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list_数媒技术1班.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a67e5402",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d8f612e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[66, 68, 76, 77, 77, 78, 78, 88, 88, 88, 88, 88, 89, 89, 90, 93]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "e1a7d31b",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.sort(reverse = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c9a26db2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[93, 90, 89, 89, 88, 88, 88, 88, 88, 78, 78, 77, 77, 76, 68, 66]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51eeee23",
   "metadata": {},
   "source": [
    "## 列表的实践（课本）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fff4dce6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "0.0\n"
     ]
    }
   ],
   "source": [
    "# 目标: \"Don't panic\"=> \"on tap\"\n",
    "# 利用列表的 方法 实现\n",
    "实践一运行前时间 = time.time()\n",
    "phrase = \"Don't panic!\"\n",
    "\n",
    "plist = list(phrase) # list()\n",
    "print(plist)\n",
    "# 删除 D\n",
    "plist.remove(\"D\")\n",
    "#print(plist)\n",
    "# 删除末尾四个元素 用pop()删除4次\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "#print(plist)\n",
    "# 删除\"'\"\n",
    "plist.pop(2)\n",
    "#print(plist)\n",
    "#plist.extend([plist.pop(),plist.pop()])# 难点在于  执行的运算先后顺序 先算圆括号，圆括号里从左往右\n",
    "#'[o', 'n', 't', ' ',]+['p'       ,       'a']\n",
    "plist.insert(4,plist.pop())\n",
    "#print(plist)\n",
    "plist.insert(2,plist.pop(3))\n",
    "#['o', 'n' , 't', 'a', 'p']\n",
    "#print(plist)\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "实践一运行后时间 = time.time()\n",
    "实践一代码的执行实践 = 实践一运行后时间-实践一运行前时间\n",
    "print(实践一代码的执行实践)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa4110aa",
   "metadata": {},
   "source": [
    "# 列表的实践二（课本）\n",
    "\n",
    "* 对比 列表的实践一 结论：\n",
    "> 1. 列表的方法 会修改 原本的列表 但是 **列表的切片** 不改变原列表\n",
    "> 2. 使用列表的方法时，需考虑是否需要先复制列表\n",
    "> 3. 思考：如何对比代码的执行效率？ 代码执行后的时间 - 代码执行前的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "da726933",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n",
      "0.0009958744049072266\n"
     ]
    }
   ],
   "source": [
    "# 目标: \"Don't panic\"=> \"on tap\"\n",
    "# 利用列表的 切片 实现\n",
    "实践二运行前时间 = time.time()\n",
    "phrase = \"Don't panic!\"\n",
    "\n",
    "plist = list(phrase) # list()\n",
    "print(plist)\n",
    "\n",
    "new_phrase=''.join(plist[1:3])+plist[5]+plist[4]+plist[7]+plist[6]\n",
    "print(new_phrase)\n",
    "实践二运行后时间 = time.time()\n",
    "实践二代码的执行实践 = 实践二运行后时间-实践二运行前时间\n",
    "print(实践二代码的执行实践)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eddd09d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1637833872.540462"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time \n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e76e758d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
